package main

import (
	"demo/influxdb/influx"
	"fmt"
	"github.com/influxdata/influxdb/client/v2"
	"math/rand"
	"time"
)

func main() {
	// 创建客户端
	httpClient, err := influx.NewClient("118.89.29.19:8086", "moyrn", "moyrn")
	if err != nil {
		panic(err)
	}

	// 选择数据库
	batchPoints, err := client.NewBatchPoints(client.BatchPointsConfig{
		Database: "device_data",
	})

	if err != nil {
		panic(err)
	}

	// 随机生成 表名 (demo)
	//clientID := uuid.TimeUUID().String()
	//fmt.Println("[clientID]", clientID)

	for {
		now := time.Now()

		// tags 带索引的列
		// field 不带索引的列
		fields := map[string]interface{}{
			"K1":  rand.Int31n(20) + 10,
			"K2":  rand.Int31n(20) + 10,
			"K3":  rand.Int31n(20) + 10,
			"K4":  rand.Int31n(20) + 10,
			"K5":  rand.Int31n(20) + 10,
			"K6":  rand.Int31n(20) + 10,
			"K7":  rand.Int31n(20) + 10,
			"K8":  rand.Int31n(20) + 10,
			"K9":  rand.Int31n(20) + 10,
			"K10": rand.Int31n(20) + 10,
			"K11": rand.Int31n(20) + 10,
			"K12": rand.Int31n(20) + 10,
		}

		// fmt.Println("[tags]", tags)
		fmt.Println("[fields]", fields)

		// tag 有索引
		point, err := client.NewPoint("1", nil, fields, now)

		if err != nil {
			panic(err)
		}

		batchPoints.AddPoint(point)

		if err := httpClient.Write(batchPoints); err != nil {
			panic(err)
		}

		time.Sleep(time.Millisecond * 100)
	}

}
